package com.topica.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.topica.CalendarActivity;
import com.topica.R;
import com.topica.TopicaApplication;
import com.topica.TopicaCourse;

public class MenuListFragment extends ListFragment {
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list_menu, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MenuListAdapter adapter = new MenuListAdapter(getActivity());
		adapter.add(new MenuListItem(getResources().getString(R.string.menu_home), R.drawable.ic_home));
		adapter.add(new MenuListItem(getResources().getString(R.string.menu_calendar), R.drawable.ic_calendar));
		adapter.add(new MenuListItem(getResources().getString(R.string.menu_event), R.drawable.ic_event));
		adapter.add(new MenuListItem(getResources().getString(R.string.menu_update), R.drawable.ic_course_update));
		adapter.add(new MenuListItem(getResources().getString(R.string.title_update_mark), R.drawable.ic_mark_update));
		MenuListItem login = new MenuListItem(getResources().getString(R.string.title_login), R.drawable.ic_login);
		MenuListItem logout = new MenuListItem(getResources().getString(R.string.log_out), R.drawable.ic_logout);
		adapter.add(login);
		adapter.add(logout);
		if(((TopicaApplication) getActivity().getApplication()).getUserId().equals("")){
			adapter.remove(logout);
		} else {
			adapter.remove(login);
		}
		setListAdapter(adapter);
	}

	private class MenuListItem {
		public String tag;
		public int iconRes;
		public MenuListItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}
	
	public class MenuListAdapter extends ArrayAdapter<MenuListItem> {

		public MenuListAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);
			convertView.setTag("item");
			return convertView;
		}

	}
	
	private void toggleMenu(){
		if(null == getActivity()){
			return;
		}
		if(getActivity() instanceof TopicaCourse){
			TopicaCourse activity = (TopicaCourse) getActivity();
			activity.toggleMenu();
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if(null == v.getTag()){
			return;
		}
		switch (position) {
		case 0:
			toggleMenu();
			break;
		case 1:
			toggleMenu();
			Intent calendar = new Intent(getActivity(), CalendarActivity.class);
			startActivity(calendar);
			break;
		default:
			break;
		}
	}
	
}
